<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/10
 * Time: 10:20
 */

namespace Biz\service;

use Biz\cache\CategoryEntrance as cCategoryEntrance;
use Biz\cache\CategorySceneTags;
use Biz\model\ShopCategoryEntrance as mShopCategoryEntranc;
use Biz\model\ShopCategoryScene;
use Doraemon\tools\ImgUrl;
use Biz\validate\CategoryEntrance as vCategoryEntrance;
use Biz\tools\SensorsApi as tSensorsApi;
use Biz\tools\Link as tLink;
/**
 * @deprecated
 */
class ShopCategoryEntrance
{
    const CATEGORY_LINK = 'hhz://mall_category_goods_list';
    const CATEGORY_ENTRANCE_CACHHE_NAME = 'categoryentrance';
    const CATEGORY_ENTRANCE = 'category';

    public static function getCategory($time)
    {

        $cacheCategoryData = cCategoryEntrance::getCategoryEntrance(self::CATEGORY_ENTRANCE_CACHHE_NAME);
        if (is_null($cacheCategoryData)) {

            $categoryData = self::getCategoryEntrance($time);

            foreach($categoryData as $info){
                self::formatInfos($info);
                $cacheCategoryData[] = $info;

            }

            cCategoryEntrance::setCategoryEntrance(self::CATEGORY_ENTRANCE_CACHHE_NAME, $cacheCategoryData);
        }

        return $cacheCategoryData ?: [];
    }

    public static function getCategory_347($time)
    {
        $categoryData = cCategoryEntrance::getCategoryEntrance(self::CATEGORY_ENTRANCE);
        if(is_null($categoryData)) {

            $list = mShopCategoryEntranc::getCategory_347($time);

            foreach($list as $info){
                self::formatInfos($info);
                $categoryData[] = $info;

            }

            cCategoryEntrance::setCategoryEntrance(self::CATEGORY_ENTRANCE, $categoryData);
        }
        return $categoryData ? : [];
    }

    public static function admGetCategoryEntranceInfoById($id)
    {
        $categoryEntranceInfo = mShopCategoryEntranc::getCategoryEntranceInfoById($id);
        $categoryEntranceInfo['banner_hash'] = $categoryEntranceInfo['banner'];
        self::formatInfos($categoryEntranceInfo);
        return $categoryEntranceInfo;
    }

    public static function admGetCategoryEntranceList($limit)
    {
        $categoryEntranceData = mShopCategoryEntranc::getCategoryEntranceList($limit);

        foreach($categoryEntranceData as $info){
            self::formatInfos($info);
            $categoryEntranceList[] = $info;

        }

        $sensorsdataConf = tSensorsApi::getCourierBirdConfig('api');
        $product = (isset($_SERVER['APP_ENV']) && $_SERVER['APP_ENV'] == "production") ? 0 : 0;
        if($product) {
            $categoryEntranceList = array_map(function ($v) use($sensorsdataConf, $product){
                $clicks  = 0;
                $clicks = tSensorsApi::clicks('nav_click', 'mall_category', 'part_name', $v['title'], $sensorsdataConf);
                $v['clicks'] = $clicks ? : 0;
                return $v;
            }, $categoryEntranceList);
        }


        $result = [
            'categoryEntranceList' => $categoryEntranceList,
            'categoryEntranceCount' => mShopCategoryEntranc::getCategoryEntranceCount($order = [], $limit),
        ];

        return $result;
    }

    public static function admAddCategoryEntrance($data, $adminUid)
    {
        $scenes = 'create';
        if (!vCategoryEntrance::validate($data, $scenes)) {
            $msg = vCategoryEntrance::firstError();
            die($msg);
        }

        $categoryEntranceInfo = [
            'banner' => $data['banner'],
            'title' => $data['title'],
            'start_time' => $data['start_time'],
            'category_pos' => $data['category_pos'],
            'link_dest' => $data['link'],
            'link' => self::CATEGORY_LINK . ':' . $data['link'],
            'addtime' => date('Y-m-d H:i:s', time()),
            'sort' => $data['sort'],
            'admin_id' => $adminUid
        ];

        $categoryEntranceId = mShopCategoryEntranc::addCategoryEntrance($categoryEntranceInfo);

        return $categoryEntranceId;
    }

    public static function admUpdateCategoryEntranceInfoById($CategoryEntranceId, $data, $adminUid)
    {
        $scenes = 'update';
        if (!vCategoryEntrance::validate($data, $scenes)) {
            $msg = vCategoryEntrance::firstError();
            die($msg);
        }

        $CategoryEntranceInfo = [
            'banner' => $data['banner'],
            'title' => $data['title'],
            'start_time' => $data['start_time'],
            'category_pos' => $data['category_pos'],
            'link_dest' => $data['link'],
            'link' => self::CATEGORY_LINK . ':' . $data['link'],
            'sort' => $data['sort'],
            'admin_id' => $adminUid
        ];

        $CategoryEntranceId = mShopCategoryEntranc::updateCategoryEntranceInfoById($CategoryEntranceId, $CategoryEntranceInfo);

        return $CategoryEntranceId;
    }

    public static function admGetCategoryEntrancePage($categoryEntranceId)
    {
        $result = mShopCategoryEntranc::getCategoryEntrancePage();
        $categoryKey = array_search($categoryEntranceId, $result);
        $categoryKey++;
        return $categoryKey;
    }

    public static function getCategoryEntrance($time)
    {
        return mShopCategoryEntranc::getCategory($time);
    }

    public static function amdStateCategoryEntranceById($categoryEntranceId)
    {
        return mShopCategoryEntranc::changeStatus($categoryEntranceId, mShopCategoryEntranc::STATUS_NORMAL);
    }

    public static function admOffLineCategoryEntranceById($categoryEntranceId)
    {
        return mShopCategoryEntranc::changeStatus($categoryEntranceId, mShopCategoryEntranc::STATUS_OFFLINE);
    }

    public static function formatInfos(&$info)
    {
        foreach ($info as $key => $value) {
            switch ($key) {
                case 'banner' :
                    $info['banner'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $info['banner'], ImgUrl::IMAGE_SIZE_SQ_PHONE);
                    break;
            }
        }
    }

    public static function saveCategorySceneTags($sceneTag, array $categroyNames, $id = 0)
	{
		if ($sceneTag && $categroyNames) {
			if ($id) {
				return ShopCategoryScene::updateSceneTags($id, $sceneTag, $categroyNames);
			} else {
				return ShopCategoryScene::addSceneTags($sceneTag, $categroyNames);
			}
		}

		CategorySceneTags::delItem('');
		return false;
	}

	public static function getAllCategorySceneTags($id = 0)
	{
		$sceneTagsInfo = ShopCategoryScene::getSceneTags($id);

		return $sceneTagsInfo;
	}

	public static function getCategoryBySceneTags($imgTags)
	{
		$categoryNames = [];
		$allCategorySceneTags = CategorySceneTags::getItem('');
		if (!$allCategorySceneTags) {
			$allCategorySceneTags = self::getAllCategorySceneTags();
			CategorySceneTags::setItem('', $allCategorySceneTags);
		}

		foreach ($allCategorySceneTags as $allCategorySceneTag) {
			if ( in_array( $allCategorySceneTag['scene_name'], $imgTags ) ) {
				$categoryNames = array_merge($categoryNames, explode(',', $allCategorySceneTag['categorys_name']));
			}
		}
		return $categoryNames;
	}

}